<?php
namespace API\Controller;
use Think\Controller;
header("Content-type: text/json; charset=utf-8"); 
class ArticlesController extends Controller {

/*1.获取所有文章信息*/
public function getArticleslist(){
        //调用模型层获取数据
        $articlesmodel=M('articles');  
        $articles=$articlesmodel->table('vm_articles a,vm_users u')->where("a.u_id=u.u_id")->field('a.*,u.u_name')->order('a_time desc')->select();
        echo json_encode($articles);
    }


/*2.获取某一条文章的详情*/
     public function getDetail(){
      $a_id=$_GET['a_id'];
      $articlesmodel=M('articles');
      $articles=$articlesmodel->table('vm_articles a , vm_type t, vm_users u')->where("a_id=$a_id and a.u_id=u.u_id and a.t_id=t.t_id")->field('a.*,t.t_name,u.u_name')->find($a_id); 
      $this->articles=$articles;
      echo json_encode($articles);
    } 



  /*3.用户发帖子*/
     public function addarticles(){
    $data['u_id']=$_REQUEST['u_id'];
     $data['a_name']=$_REQUEST['a_name'];
    $data['a_info']=$_REQUEST['a_info'];
    if(!empty($data['a_info'])&&!empty($data['u_id'])){
          $data['a_time'] = array('exp','now()'); 
          if($_FILES['file']['size']>0){
            $filename = explode('.', $_FILES['file']['name']);
            $savename = rand(0,99).time().'.'.end($filename);
            move_uploaded_file($_FILES['file']['tmp_name'],'./Public/Admin/vmphoto/'.$savename);
            $data['a_pic'] = $savename;
          }
           $articles = M('articles');
         // $articles = M(CONTROLLER_NAME);
           if(false!=$articles->add($data)){
            $senting = array('success'=>1);
            echo json_encode($senting);
           }else{
             $senting = array('success'=>0);
            echo json_encode($senting);
           }
    }else{
             $senting = array('success'=>404);
             echo json_encode($senting);
    }
    
}


 /*4.文章评论列表*/
 public function getArticlesCommentlist()
    {
         $a_id=$_GET['a_id'];
        $flag=$_GET['flag'];
        $commentsmodel=M('comments');
    
        $comments=$commentsmodel->table('vm_comments ct,vm_articles p,vm_users u')->where("p.a_id=$a_id and ct.comment_id=p.a_id and ct.u_id=u.u_id and ct.comment_flag=$flag")->order('com_id desc')->field('p.a_id,u.u_id,u.u_name,ct.*')->select();
        echo json_encode($comments);        
    }


 /*5.查看每日经典台词*/
  public function words(){
     $wordsmodel=M('words');
     $sql = 'select *from vm_words order by rand() LIMIT 1';
     $words =M('words')->query($sql);
    // $this->ajaxReturn($words,'JSON');
     echo json_encode($words); 
}

   


 /*6.发表评论*/

//  public function addArticlesComment(){
//     $data['a_id']=$_GET['a_id'];
//     $data['u_id']=$_GET['u_id'];
//     $data['comment']=$_GET['info'];
//     $data['creattime'] =  date("Y-m-d H:i:s"); 
//     $commentsmodel=M('comments');                                         
//     $comments=$commentsmodel->add($data);

//      if(false !=$comments){          
//         echo('{"success":"1"}');
//     }else{
//         //$this->false;
//         echo('{"success","0"}');
//     } 

// }


    }
    

